import { useI18n } from '@/hooks/web/useI18n'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { useValidator } from '@/hooks/web/useValidator'
import { useEnumeratePicker } from '@/utils'
import { ElDivider, ElInput, ElSwitch, ElTable, ElTableColumn } from 'element-plus'

const { t } = useI18n()

const { required } = useValidator()
export default function (schemaParams: any = {}) {
  const crudSchemas = reactive<CrudSchema[]>([
    {
      field: 'inventory_id',
      label: 'ID',
      width: 80,
      align: 'center',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center'
      }
    },
    {
      field: 'product',
      label: t('inventory.cname'),
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        },
        formItemProps: {}
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.product_name
          }
        }
      }
    },
    {
      field: 'hashrate',
      label: t('inventory.hashRate'),
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        },
        formItemProps: {}
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.hashRate
          }
        }
      }
    },
    {
      field: 'product_code',
      label: '商品编码',
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        },
        formItemProps: {}
      },
      table: {
        hidden: true
      }
    },
    {
      field: 'wid',
      label: t('inventory.warehouse'),
      search: {
        component: 'TreeSelect',
        componentProps: {
          checkStrictly: true,
          filterable: true,
          data: schemaParams.warehouseList ? schemaParams.warehouseList : []
        }
      },
      form: {
        formItemProps: {
          slots: {
            default: (data: any) => {
              return <ElInput v-model={data.warehouse_name} disabled={schemaParams.index === 2} />
            }
          }
        }
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return useEnumeratePicker(schemaParams?.wid || [], data.row.wid)
          }
        }
      }
    },
    {
      field: 'is_futures',
      label: t('inventory.is_futures'),
      search: {
        component: 'Select',
        componentProps: {
          options: schemaParams.is_futuresList ? schemaParams.is_futuresList : []
        }
      },
      form: {
        hidden: true,
        component: 'Input'
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return useEnumeratePicker(schemaParams?.is_futuresList || [], data.row.is_futures)
          }
        }
      }
    },
    {
      field: 'time',
      label: '到货日期',
      search: {
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          type: 'daterange',
          unlinkPanels: true,
          rangeSeparator: '-'
        }
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        },
        formItemProps: {}
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.arrival_date
          }
        }
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'real_arrival_date',
      label: '实际到货日期',
      search: {
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          type: 'daterange',
          unlinkPanels: true,
          rangeSeparator: '-'
        }
      }
    },
    {
      field: 'quantity',
      label: '库存汇总',
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        },
        formItemProps: {}
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.quantity
          }
        }
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'currency',
      label: t('inventory.currency'),
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return useEnumeratePicker(schemaParams?.currencyList || [], data.row.currency)
          }
        }
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'original_price',
      label: '售价',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return data.row.original_price
          }
        }
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'action',
      width: schemaParams.actionButtonsTemplate
        ? (schemaParams.actionButtonsTemplate?.length || 3) * 60 + 20 + 'px'
        : 'auto',
      label: t('tableDemo.action'),
      fixed: 'right',
      search: {
        hidden: true
      },
      form: {
        hidden: true
      },
      detail: {
        hidden: true
      },
      table: {
        headerAlign: 'center',
        align: 'center',
        slots: {
          default: (data: any) => {
            return schemaParams.actionButtonsTemplate
              ? schemaParams.actionButtonsTemplate?.template(data)
              : null
          }
        }
      }
    },
    //编辑仓库表单配置
    {
      field: 'arrival_batch',
      label: '批次',
      search: {
        hidden: true
      },
      form: {
        hidden: true
        // component: 'Input',
        // componentProps: {
        //   disabled: schemaParams.index === 2
        // }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'futures',
      label: '库存类型',
      search: {
        hidden: true
      },
      form: {
        component: 'Input',
        componentProps: {
          disabled: schemaParams.index === 2
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    //采购成本 table
    {
      field: 'procurementCosts',
      label: '采购成本',
      search: {
        hidden: true
      },
      form: {
        colProps: {
          span: 24
        },
        formItemProps: {
          slots: {
            default: (data: Record<string, any>) => {
              return (
                (schemaParams.index === 2 && (
                  <ElTable
                    data={data.purchase || []}
                    highlight-current-row
                    stripe
                    border
                    cellStyle={{ textAlign: 'center' }}
                    headerCellStyle={{ textAlign: 'center' }}
                  >
                    <ElTableColumn
                      label="供应商"
                      min-width="200"
                      prop="supname"
                      showOverflowTooltip={true}
                    />
                    <ElTableColumn
                      label="关联销售订单编码"
                      min-width="200"
                      prop="sales_orderno"
                      showOverflowTooltip={true}
                    />
                    <ElTableColumn
                      label="采购单编码"
                      min-width="200"
                      prop="purchase_no"
                      showOverflowTooltip={true}
                    />
                    <ElTableColumn
                      label="采购成本"
                      min-width="200"
                      prop="cost"
                      showOverflowTooltip={true}
                    />
                    <ElTableColumn
                      label="采购日期"
                      min-width="200"
                      prop="order_time"
                      showOverflowTooltip={true}
                    />
                  </ElTable>
                )) ||
                null
              )
            }
          }
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      field: 'is_sale',
      label: '是否可售',
      search: {
        hidden: true
      },
      form: {
        component: 'Select',
        componentProps: {
          options: schemaParams.saleOptions || []
        },
        formItemProps: {
          rules: [required()]
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    },
    {
      search: {
        hidden: true
      },
      form: {
        formItemProps: {
          slots: {
            default: () => <ElDivider></ElDivider>
          }
        },
        colProps: {
          span: 24
        }
      },
      table: {
        hidden: true
      },
      detail: {
        hidden: true
      }
    }
  ])

  return {
    crudSchemas
  }
}
